package View;
/*
 * Written by Phat Le
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import Model.Constants;
import Model.ProgramChair;
import TableModels.TemplateTableModel;;
/**
 * Creates view for pc to make approval.
 * @author Phat
 *
 */
public class MakeApprovalView extends JFrame{
	private static final long serialVersionUID = 1L;  //to get rid of warning
	private static final int WIDTH = 300;
	private static final int HEIGHT = 500;
	private static final int BUTTON_WIDTH = 100;
	private static final int BUTTON_HEIGHT = 30;
	private static final int TOP_PANEL_HEIGHT = 3;
	private static final int MID_PANEL_HEIGHT = 10;
	private static final int BOT_PANEL_HEIGHT = 3;
	private static final int FRAME_HEIGHT = 16;
	
	
	private JLabel title;
	private JLabel author;
	private JComboBox recommendBox;
	private JTable table;
	
	private String title_Text;
	private String author_Text;
	private Object apr_Value;
	
	private final JFrame mainFrame;
	private ProgramChair pc;
	
	public MakeApprovalView(final JTable a_table,  final ProgramChair a_pc) {
		table = a_table;
		pc = a_pc;
		mainFrame = new JFrame("Make Approval");

	}
	
	public void start() {
	
		mainFrame.setSize(WIDTH, HEIGHT);
		mainFrame.setResizable(false);
		mainFrame.setLocationRelativeTo(null);		//set in the middle of the screen
		mainFrame.setLayout(new BorderLayout());
		mainFrame.add(createTopPanel(), BorderLayout.NORTH);  //use the enum Roles for the role string
		mainFrame.add(createMidPanel(), BorderLayout.WEST);
		mainFrame.add(createBotPanel(), BorderLayout.SOUTH);
		mainFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		mainFrame.setVisible(true);
	}
	
	/**
	 * Creates the top panel that contains name, conference, & role info.
	 */
	private JPanel createTopPanel() {
		
		JPanel topPnl = new JPanel();
		topPnl.setBackground(Color.LIGHT_GRAY);	
		topPnl.setPreferredSize(new Dimension(WIDTH, TOP_PANEL_HEIGHT * HEIGHT / FRAME_HEIGHT));	//set this panel to 2/16 of the JFrame
		//topPnl.setLayout(new GridLayout(3,1,5,5)); //3 rows, 1 columns, hgap = 5, vgap = 5
		JLabel nameLbl = new JLabel("Acceptance Decision", JLabel.CENTER);      //create labels for user info 
		topPnl.add(nameLbl);
		add(topPnl);	
		return topPnl;
	}
	

	private JPanel createMidPanel() {
		JPanel botLftPnl = new JPanel();
		botLftPnl.setBackground(Color.WHITE);
		botLftPnl.setPreferredSize(new Dimension(WIDTH, MID_PANEL_HEIGHT * HEIGHT / FRAME_HEIGHT));  //set this panel to 3/16 of the JFrame
		FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
		layout.setVgap(30);										//vertical gap for the buttons
		layout.setHgap(25);										//horizontal gap for the buttons
		botLftPnl.setLayout(layout);
		
		JLabel titleLbl = new JLabel("Title: ");
		titleLbl.setPreferredSize(new Dimension(100, 20));
		title = new JLabel();
		title.setPreferredSize(new Dimension(100, 20));
	
		JLabel authorLbl = new JLabel("Author: ");
		authorLbl.setPreferredSize(new Dimension(100, 20));
		author = new JLabel();
		author.setPreferredSize(new Dimension(100, 20));
		
		JLabel recommendLbl = new JLabel("Approval Status:");
		recommendLbl.setPreferredSize(new Dimension(100, 20));
		recommendBox = new JComboBox(Constants.values());
		recommendBox.setPreferredSize(new Dimension(100, 20));
		recommendBox.setEditable(false);
	
		
		botLftPnl.add(titleLbl);
		botLftPnl.add(title);
		botLftPnl.add(authorLbl);
		botLftPnl.add(author);
		botLftPnl.add(recommendLbl);
		botLftPnl.add(recommendBox);

		add(botLftPnl);
		return botLftPnl;
	}
	
	/**
	 * @return
	 */

	private JPanel createBotPanel() {
		JPanel botRgtPnl = new JPanel();
		botRgtPnl.setBackground(Color.LIGHT_GRAY);
		botRgtPnl.setPreferredSize(new Dimension(WIDTH / 2, BOT_PANEL_HEIGHT * HEIGHT / FRAME_HEIGHT));  //set this panel to 3/16 of the JFrame
		FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
		layout.setVgap(30);										//vertical gap for the buttons
		layout.setHgap(25);										//horizontal gap for the buttons
		botRgtPnl.setLayout(layout);
		
		JButton submitBut = new JButton("Make");
		submitBut.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
		submitBut.addActionListener(new ActionListener()
	      {
	        public void actionPerformed(final ActionEvent the_event){
	        		/*TemplateTableModel model = (TemplateTableModel)table.getModel();
		        	int row = table.convertRowIndexToModel(table.getSelectedRow());
		        	//save old manuscript
					Manuscript oldM = model.getManuscript(row);  //store for using as a parameter
					Manuscript newM = null;		*/	
		        	int aprValue = ((Constants) recommendBox.getSelectedItem()).getValue();
		        	pc.makeApproval(title_Text, aprValue);
		        	/*newM = pc.modifyPaper(oldM, aprValue);
			    	if(newM != null) {
			    		model.setManuscript(newM, row);   //copy new manuscript to tablemodel
			    		table.repaint();
			    		JOptionPane.showMessageDialog(null, "Acceptane decision is made");
			    	}else{
			    		JOptionPane.showMessageDialog(null, "Update Failed");
			    	}*/
		        	//JOptionPane.showMessageDialog(null, "Acceptane decision is made");
		        	mainFrame.dispose();
		        
	        }
	    });
		JButton cancelBut = new JButton("Cancel");
		cancelBut.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
		cancelBut.addActionListener(new ActionListener()
	      {
	        public void actionPerformed(final ActionEvent the_event){
	        	mainFrame.dispose();
	        }
	      });
	
		botRgtPnl.add(submitBut);
		botRgtPnl.add(cancelBut);

		add(botRgtPnl);
		return botRgtPnl;
	}
	
	/**
	 * 
	 */
	public void updateLabels() {
		
		if(table.getSelectedRow() != -1) {
			//convert to models row index, since the table index changes when it is sorted
			int row = table.convertRowIndexToModel(table.getSelectedRow());
			//must cast to correct TableModel, or getManuscriptLink wont work.
			title_Text = (String) ((TemplateTableModel)table.getModel()).getValueAt(row, 0);//get link
			author_Text = (String) ((TemplateTableModel)table.getModel()).getValueAt(row, 1);
			apr_Value =  ((TemplateTableModel)table.getModel()).getValueAt(row, 7);
		}
		
		title.setText(title_Text);
		author.setText(author_Text);
		recommendBox.setSelectedItem(apr_Value);
	}
	
	
}

